/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.deptBudget.bean;

import com.ibatis.dao.client.DaoManager;
import com.yunhoo.base.model.Department;
import com.yunhoo.base.model.Project;
import com.yunhoo.deptBudget.db.dao.FinBudgetAddDao;
import com.yunhoo.deptBudget.model.Budget;
import com.yunhoo.sys.db.config.DaoConfig;
import com.yunhoo.sys.util.common.CommonParam;
import com.yunhoo.sys.util.common.LoginParam;
import com.yunhoo.sys.util.common.Null;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;

@ManagedBean(name = "budgetAddBean")
@SessionScoped
public class FinBudgetAddBean implements Serializable {

    private static DaoManager daom;
    private static FinBudgetAddDao dao;
    private Budget edit = new Budget();
    private List<Budget> datali = null;//新建 追加编制
    private List<Budget> editli = null;//新建 追加编制
    private DataModel<Budget> updateModel = new ListDataModel<Budget>();//已存在 的 预算追加 数据列表
    private List<Budget> deleteli = null;//待删除 预算追加 数据列表
    private boolean addOrUpdate = false;
    private boolean editStatusNew = false;
    private boolean editStatusUpdate = false;
    //
    private DataModel<Project> finProModel = new ListDataModel<Project>();//财务项目
    private Project finPro = null;
    //
    private List<SelectItem> deptli = null;
    private Department dept = null;

    public FinBudgetAddBean() {
        daom = DaoConfig.getDaoManager();
        dao = (FinBudgetAddDao) daom.getDao(FinBudgetAddDao.class);
        dept = new Department();
        initDeptli();
        initList();
    }

    private void initDeptli() {
        deptli = CommonParam.deptListOfSelect();
    }

    public void deptChange() {
        String deptCode = dept.getCode().trim();
        for (SelectItem item : deptli) {
            System.out.println("item : " + item.getLabel() + " -- " + item.getValue());
            if (item.getValue().toString().trim().equals(deptCode)) {
                dept.setName(item.getLabel());
                System.out.println("itemLabel : " + item.getLabel());
            }
        }
        System.out.println("deptCode : " + dept.getCode() + " -- " + dept.getName());
    }

    private void initStatus() {
        addOrUpdate = false;
        editStatusNew = false;
        editStatusUpdate = false;
    }

    private void initList() {
        List<Project> finProli = CommonParam.projectList();
        finProModel.setWrappedData(finProli);
    }

    public void finProSelectedForNew() {
        editli = new ArrayList<Budget>();
        for (Project fin : finProModel) {
            if (fin.isSelected()) {
                Budget be = new Budget(true);
                be.setProjCode(fin.getCode());
                be.setProjName(fin.getName());
                be.setDeptCode(dept.getCode());
                be.setDeptName(dept.getName());
                editli.add(be);
            }
        }
        editStatusNew = true;
    }

    public void finProSelectedForEdit() {
        String code = "";
        for (Project fin : finProModel) {
            if (fin.isSelected()) {
                code += fin.getCode().trim() + ",";
            }
        }
        if (!code.isEmpty()) {
            code = code.substring(0, code.length() - 1);
            Budget param = new Budget(true);
            param.setProjCode(code);
            param.setDeptCode(dept.getCode());
            try {
                datali = dao.queryList(param);
            } catch (SQLException ex) {
                Logger.getLogger(FinBudgetAddBean.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (!Null.check(datali)) {
                formatli(datali);
            }
            updateModel.setWrappedData(datali);
            deleteli = new ArrayList<Budget>();
            editStatusUpdate = true;
        }
    }

    public void delete() {
        int index = updateModel.getRowIndex();
        if (!Null.check(datali)) {
            formatli1(datali);
        }
        deleteli.add(datali.get(index));
        datali.remove(index);
        if (!Null.check(datali)) {
            formatli(datali);
        }
        updateModel.setWrappedData(datali);
    }

    private void formatli1(List<Budget> datali) {
        String finCode = "";
        String finName = "";
        for (Budget data : datali) {
            if (!finCode.equals(data.getProjCode().trim())) {
                finCode = data.getProjCode().trim();
                finName = data.getProjName();
            } else {
                data.setProjName(finName);
            }
        }
    }

    private void formatli(List<Budget> datali) {
        String finCode = "";
        for (Budget data : datali) {
            if (!finCode.equals(data.getProjCode().trim())) {
                finCode = data.getProjCode().trim();
            } else {
                data.setProjName("");
            }
        }
    }

    private String loginYear() {
        return LoginParam.getIyear().toString();
    }

    public void add() {
        addOrUpdate = true;
        //editStatusNew = true;
    }

    public void update() {
        addOrUpdate = false;
        //editStatusUpdate = true;
    }

    public void checkSaveNew() {
        List<Budget> templi = new ArrayList();
        for (Budget row : editli) {
            if (row.getBudget() != null && row.getBudget() != 0) {
                templi.add(row);
            }
        }
        if (templi.isEmpty()) {
            return;
        }
        try {
            daom.startTransaction();
            dao.insertList(templi);
            daom.commitTransaction();
        } catch (SQLException ex) {
            daom.endTransaction();
            Logger.getLogger(FinBudgetAddBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        initStatus();
    }

    public void checkSaveUpdate() {
        List<Budget> templi = (ArrayList<Budget>) updateModel.getWrappedData();
        try {
            daom.startTransaction();
            dao.deleteListById(deleteli);
            dao.updateListById(templi);
            daom.commitTransaction();
        } catch (SQLException ex) {
            daom.endTransaction();
            Logger.getLogger(FinBudgetAddBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        initStatus();
    }

    public void cancel() {
        if (addOrUpdate) {
            edit = new Budget();
        }
        initStatus();
    }

    public static DaoManager getDaom() {
        return daom;
    }

    public static void setDaom(DaoManager daom) {
        FinBudgetAddBean.daom = daom;
    }

    public static FinBudgetAddDao getDao() {
        return dao;
    }

    public static void setDao(FinBudgetAddDao dao) {
        FinBudgetAddBean.dao = dao;
    }

    public Budget getEdit() {
        return edit;
    }

    public void setEdit(Budget edit) {
        this.edit = edit;
    }

    public List<Budget> getDatali() {
        return datali;
    }

    public void setDatali(List<Budget> datali) {
        this.datali = datali;
    }

    public List<Budget> getEditli() {
        return editli;
    }

    public void setEditli(List<Budget> editli) {
        this.editli = editli;
    }

    public DataModel<Budget> getUpdateModel() {
        return updateModel;
    }

    public void setUpdateModel(DataModel<Budget> updateModel) {
        this.updateModel = updateModel;
    }

    public List<Budget> getDeleteli() {
        return deleteli;
    }

    public void setDeleteli(List<Budget> deleteli) {
        this.deleteli = deleteli;
    }

    public boolean isAddOrUpdate() {
        return addOrUpdate;
    }

    public void setAddOrUpdate(boolean addOrUpdate) {
        this.addOrUpdate = addOrUpdate;
    }

    public boolean isEditStatusNew() {
        return editStatusNew;
    }

    public void setEditStatusNew(boolean editStatusNew) {
        this.editStatusNew = editStatusNew;
    }

    public boolean isEditStatusUpdate() {
        return editStatusUpdate;
    }

    public void setEditStatusUpdate(boolean editStatusUpdate) {
        this.editStatusUpdate = editStatusUpdate;
    }

    public DataModel<Project> getFinProModel() {
        return finProModel;
    }

    public void setFinProModel(DataModel<Project> finProModel) {
        this.finProModel = finProModel;
    }

    public Project getFinPro() {
        return finPro;
    }

    public void setFinPro(Project finPro) {
        this.finPro = finPro;
    }

    public List<SelectItem> getDeptli() {
        return deptli;
    }

    public void setDeptli(List<SelectItem> deptli) {
        this.deptli = deptli;
    }

    public Department getDept() {
        return dept;
    }

    public void setDept(Department dept) {
        this.dept = dept;
    }
}
